const tokenObj: any = window.localStorage.getItem("LoginState");

let token: string ='';

if(tokenObj) {
  token = JSON.parse(tokenObj).token;
}
const baseUrl = import.meta.env.VITE_FETCH_URL;

async function myFetch(
  url: String,
  method: "GET" | "POST" | "PUT" | "DELETE" | "etc",
  data?: {}
) {
  const response = await fetch(`${baseUrl}${url}`, {
    method: method,
    mode: "cors",
    cache: "no-cache",
    credentials: "same-origin",
    headers: {
      "Content-Type": "application/json",
      Authorization: token,
    },
    redirect: "follow",
    referrerPolicy: "no-referrer",
    body: JSON.stringify(data),
  });
  return response.json();
}

export default myFetch;
